<?php
namespace App\Models;

use App\Common\tbCommon;
use App\Models\ImageM;
use DB;
use Illuminate\Database\Eloquent\Model;

class TagM extends Model
{

    public  static   $STable="lee_image_tag";
    protected  $table="lee_image_tag";
    
    
    /**
     * tag表中添加记录
     * @param uuid $imageid image的id值
     * @param array $data 存入数据
     * 如：['pox_x'=>x,'pos_y'=>y,'price'=>3,
     *      'brand'=>'sdf','currency'=>'zh']
     */
    static  public function addItem($imageid,array $data){
        $tagInst=new TagM();
        $tagInst->imagefid=$imageid;
        foreach ($data as $key => $value){
            $tagInst->$key=$value;
        }
        $tagInst->save();
        
    }
    
    
    /**
     * 添加多条数据记录
     * @param array $datas,输入数据 二维数组[array(),array()]
     * 如 
     * [ 
     *   //image1 tag1 item
     *   ['imageid'=>imageid1,'pox_x'=>x,'pos_y'=>y,'price'=>3,'brand'=>'sdf','currency'=>'zh'],
     *   // image1 tag2 item
     *   ['imageid'=>imageid1,'pox_x'=>x,'pos_y'=>y,'price'=>3,brand'=>'sdf','currency'=>'zh'],
     *   // image2 tag1 item
     *   ['imageid'=>imageid2,'pox_x'=>x,'pos_y'=>y,'price'=>3,brand'=>'sdf','currency'=>'zh']
     * 
     * ]
     *    
     */
    static public function addItems(array $datas){
        // 批量插入数据
        $tbName=TagM::$STable;
        $newDatas=tbCommon::addMissColumnValue($tbName, $datas);
        DB::table($tbName)->insert($newDatas);
    }
    
    /**
     * 删除单个数据记录
     * @param uuid $id,待删除数据的主键
     */
    static public function deltItem($id){
        TagM::destroy($id);
    }
    
    /**
     * 批量删除
     * @param array $ids,待删除数据的 id集合
     * 如： array(id1,id2,id3)
     */
    static public function deltItems(array $ids){
        TagM::destroy($ids);
    }
    
    /**
     * 依据 image的id值删除数据单个image的tag
     * @param uuid $imgId,image的uuid值
     */
    static public function deltByImgId($imgId){
        TagM::where('imageid',$imgId)->delete();
    }
    
    
    /**
     * 依据 image的id集合   批量删除所有image的tag
     * @param array $imgId,image的uuid值
     */
    static public function deltByImgIds(array $imgIds){
        TagM::whereIn('imageid',$imgIds)->delete();
    }
    

    /**
     * 依据 image的fid返回 对应的tag记录
     * @param bigint $imgfid,image的fid值
     */
    static public function getItemsByImgFid($imgfid){
       $imgTable=ImageM::$STable;
       $tagTable=self::$STable;
        
        $sql=" select tg.id as tgid  from $imgTable as img,$tagTable as tg
            where img.fid= $imgfid and tg.imageid=img.id";
        return DB::select($sql);
    }
    
    
    /**
     * 依据 image的fid集合 返回 对应的tag记录
     * @param array $imgfids,image的fid集合
     * 如 [imgfid1,imgfid2,imgfid3]
     *返回项 包括 imgfid（image的fid）,tgid（tag的id）,
     * tgx（tag的x偏移量）,tgy（tag的y偏移量）,itemtype（tag的单品类型）,itemid（tag的单品id）
     * itemlink（单品的链接）
     */
    static public function getItemsByImgFids(array $imgfids){
        $imgTable=ImageM::$STable;
        $tagTable=self::$STable;
        $imgfidStr=implode(',', $imgfids);
        $sql="select img.fid as imgfid ,
            tg.id as tgid,tg.pos_x as tgx,tg.pos_y as tgy,tg.itemtype as itemtype,
            tg.itemid as itemid,tg.itemlink as itemlink,
            tg.brand as brand,tg.price as price,tg.currency as currency,
            tg.region as region,tg.description as description
            from $imgTable as img,$tagTable as tg
            where img.fid in ( $imgfidStr ) and tg.imageid=img.id";
        return DB::select($sql);
    }
    
}

?>